/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.evaluation.onsite;

import cn.ac.nercis.pes.common.constant.common.BaseConstants;
import cn.ac.nercis.pes.common.constant.knowledge.EvaluationMethod;
import cn.ac.nercis.pes.common.utils.ConvertUtils;
import cn.ac.nercis.pes.community.component.dialog.PageDialog;
import cn.ac.nercis.pes.community.controller.base.GlobalEventHandle;
import cn.ac.nercis.pes.community.controller.evaluation.planning.EvalMethodFastController;
import cn.ac.nercis.pes.community.manager.RealTimeSaveManager;
import cn.ac.nercis.pes.community.support.event.CommonCallback;
import cn.ac.nercis.pes.community.utils.AppStore;
import cn.ac.nercis.pes.community.views.evaluation.planning.EvalMethodFastView;
import cn.ac.nercis.pes.model.dto.knowledge.KmSimpleTargetDTO;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 现场测评内容管理（基础）
 * @author zpy
 * @version 1.0.0
 */
public abstract class BaseEvaluationController implements Initializable, GlobalEventHandle {
    protected final static Integer MAX_SHOW_MENU_TEXT_LENGTH = 15;
    protected final SimpleBooleanProperty dataSaving = new SimpleBooleanProperty(false);
    protected final RealTimeSaveManager realTimeSaveManager = new RealTimeSaveManager(this::realTimeSave);

    protected void bindVisibleAndManage(Node... nodes){
        Stream.of(nodes).forEach(n->n.managedProperty().bind(n.visibleProperty()));
    }

    protected void changeResultTab(Node showTab, Node... hideTabs){
        Stream.of(hideTabs).forEach(n->n.setVisible(false));
        showTab.setVisible(true);
    }

    /**
     * 即时保存
     */
    public abstract void realTimeSave();

    protected void showAssetEvalMethod(String systemId, String assetId, KmSimpleTargetDTO layerTarget, String unitTargetId, CommonCallback saveCallback){
        var showView = AppStore.getEvaluationView(systemId, EvalMethodFastView.class);
        PageDialog dialog = PageDialog.builder()
                .withIcon("check-icon")
                .withTitle("查看对象测评说明")
                .withContent(Objects.requireNonNull(showView).load())
                .withOverlayClose(false)
                .withShowActions(false)
                .withOwner(AppStore.getCurrentSystem(systemId).getEvaluationStage())
                .build();
        if(showView.getController() instanceof EvalMethodFastController controller){
            controller.initializeData(systemId,assetId,layerTarget, unitTargetId, saveCallback);
        }
        dialog.show();
    }

    protected void setEvalMethod(Label methodLbl, String method){
        if(StringUtils.isBlank(method)){
            methodLbl.setText("");
        }else{
            String evaMethod = Stream.of(StringUtils.split(method, BaseConstants.MULTI_SPLIT_CHAR))
                    .map(ConvertUtils::toInteger)
                    .map(EvaluationMethod::fromMethod)
                    .map(EvaluationMethod::getName)
                    .collect(Collectors.joining(BaseConstants.WORD_MULTI_JOIN_CHAR));
            methodLbl.setText(evaMethod);
        }
    }

    protected void setContent(VBox descBox,@NonNull String desc){
        descBox.getChildren().clear();
        Stream.of(desc.split(BaseConstants.REGEX_WRAP_TEXT_SPLIT))
                .filter(StringUtils::isNotBlank)
                .forEach(text->{
                    Label label = new Label(text);
                    label.setWrapText(true);
                    if(StringUtils.isNotBlank(text)){
                        label.setTooltip(new Tooltip(text));
                    }
                    descBox.getChildren().add(label);
                });
    }

}